package phoenix.web.page_objects.backend;

import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import core.config.Config;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.testng.TestException;
import phoenix.core.PhoenixManager;
import phoenix.core.base.extensions.PageBaseWebExt;
import phoenix.core.data_models.TestUserDataModel;
import phoenix.web.locators.backend.SearchPageLocators;

/**
 * Class provides the methods for Search page (backend)
 */
public class SearchPage extends PageBaseWebExt
{
	/**
	 * Initializes the SeaarchPage
	 * @param p_manager PhoenixManager instance
	 */
	public SearchPage(PhoenixManager p_manager)
	{
		super(p_manager);
	}

	/**
	 * Gets web elements by site
	 * @return SearchPageLocators instance
	 */
	public SearchPageLocators elements()
	{
		return new phoenix.web.locators.backend.SearchPageLocators();
	}

	public TestUserDataModel getUserData()
	{
		TestUserDataModel user = new TestUserDataModel(manager);

		user.setId(getUserID())
			.setScreenname(getUserScreenname())
			.setEmail(getUserEmail())
			.setPassword(getUserPassword())
			.setBirthDate(getUserBirthDate())
			.setRegDate(manager.testUser().getRegDate())
			.setCountry(manager.testModel().getLocation())
			.setGender(getUserGender())
			.setSexuality(getUserSexuality())
			.setAge(getUserAge())
			.setRegSource(getUserRegSource())
			.setMembershipStatus(user.defineMembershipStatus(manager.testUser().getGender()))
			.setUserKey(getUserKey())
			.setPlatform(getUserPlatform());

		return user;
	}

	public boolean isLogged()
	{
		Boolean isConnected = false;
		int i = 0;

		while (!isConnected)
		{
			if (i > 5)
			{
				break;
			}
			else
			{
				// Do another attempt
				isConnected = this.loginToBackend();

				if (isConnected)
				{
					log().info("Backend: connected");
					break;
				}
				wd().wait(10); // Wait some time
			}
			i++;
		}
		return isConnected;
	}

	/**
	 * Gets user data by POST request
	 */
	public TestUserDataModel getUserDataByPOSTRequest(String p_userID)
	{
		TestUserDataModel user = new TestUserDataModel(manager);
		Map<String, String> userData = new HashMap<String, String>();

		userData = this.getUserData(p_userID);

		user.setId(userData.get("Id"))
			.setScreenname(userData.get("Login"))
			.setEmail(userData.get("Email"))
			.setPassword(userData.get("Password"))
			.setBirthDate(userData.get("Birthday"))
			.setRegDate(manager.testUser().getRegDate())
			.setCountry(manager.testModel().getLocation())
			.setGender(userData.get("Gender"))
			.setSexuality(userData.get("Sexual orientation"))
			.setAge(userData.get("Age"))
			.setRegSource(userData.get("Traffic source"))
			.setMembershipStatus(user.defineMembershipStatus(manager.testUser().getGender()))
			.setUserKey(userData.get("Autologin key"))
			.setPlatform(userData.get("Platform"))
			.setSiteId(userData.get("Site id"))
			.setBuild(Config.BUILD);

		return user;
	}

	public String getPassword()
	{
		TestUserDataModel l_model = manager.backend().searchPage().getUserDataByPOSTRequest(manager.testUser().getId());
		return l_model.getPassword();
	}

	public String getEmail()
	{
		TestUserDataModel l_model = manager.backend().searchPage().getUserDataByPOSTRequest(manager.testUser().getId());
		return l_model.getEmail();
	}

	public String getSiteID()
	{
		TestUserDataModel l_model = manager.backend().searchPage().getUserDataByPOSTRequest(manager.testUser().getId());
		return l_model.getSiteId();
	}

	public void unmarkUsersAsHot(TestUserDataModel[] p_userModel)
	{
		for (Integer x = 0; x < p_userModel.length; x++)
		{
			manager.backend().searchPage().unsetUserAsHot(p_userModel[x]);
		}
	}

	/**
	 * Checks for is user active
	 * @return User is active or not
	 */
	@Deprecated
	public boolean isUserActive()
	{
		if (wd().getText(elements().blockUserStatus).contains("ACTIVE"))
		{
			log().debug("Check for is user active in DB => " + true);
			return true;
		}
		else
		{
			log().warn("Check for is user active in DB => " + false);
			return false;
		}
	}

	/**
	 * Gets user id
	 * @return User id
	 */
	private String getUserID()
	{
		return wd().getText(elements().blockUserID);
	}

	/**
	 * Gets user screen name
	 * @return User screen name
	 */
	private String getUserScreenname()
	{
		return wd().getText(elements().blockUserScreenname);
	}

	/**
	 * Gets user password
	 * @return User password
	 */
	public String getUserPassword()
	{
		return wd().getText(elements().blockUserPassword);
	}

	/**
	 * Gets user email
	 * @return User email
	 */
	private String getUserEmail()
	{
		return wd().getText(elements().blockUserEmail);
	}

	/**
	 * Gets user birth date
	 * @return User birth date
	 */
	private String getUserBirthDate()
	{
		return wd().getText(elements().blockUserBirthDay);
	}

	/**
	 * Gets user registration date
	 * @return User registration date
	 */
	private String getUserRegDate()
	{
		return wd().getText(elements().blockUserRegDate);
	}

	/**
	 * Gets user country
	 * @return User country
	 */
	private String getUserCountry()
	{
		return wd().getText(elements().blockUserCountry);
	}

	/**
	 * Gets user gender
	 * @return User gender
	 */
	private String getUserGender()
	{
		log().debug("Get user 'gender' from backend");

		return wd().getText(elements().blockUserGender);
	}

	/**
	 * Gets user site id
	 * @return User site id
	 */
	private String getUserSiteID()
	{
		return wd().getText(elements().blockUserSiteID);
	}

	/**
	 * Gets user key
	 * @return User key
	 */
	private String getUserKey()
	{
		return wd().getText(elements().blockUserKey);
	}

	/**
	 * Gets user platform
	 * @return User platform
	 */
	private String getUserPlatform()
	{
		return wd().getText(elements().blockUserPlatform);
	}

	/**
	 * Gets user sexuality
	 * @return User sexuality
	 */
	private String getUserSexuality()
	{
		return wd().getText(elements().blockUserSexuality);
	}

	/**
	 * Gets user age
	 * @return User age
	 */
	private String getUserAge()
	{
		return wd().getText(elements().blockUserAge);
	}

	/**
	 * Gets user reg source
	 * @return User reg source
	 */
	private String getUserRegSource()
	{
		return wd().getText(elements().blockUserRegSource);
	}

	/**
	 * Check is credits of by given value p_credits
	 * @param p_userID
	 * @param p_credits
	 * @return
	 */
	@Deprecated
	public void isCreditsOff(String p_userID, String p_credits)
	{
		log().info("Check credits...");
		manager.backend().indexPage().login();
		wd().openURL(manager.config().BACKEND_URL_BASE + "/userCredit/history?userId=" + p_userID);
		wd().waitForPresence(elements().blockCreditHistory, "Block 'Credit Histrory doesn't appear'");
		String credit_actual = wd().getText(elements().blockCreditValue);
		String credit_expected = "-" + p_credits + ".";
		if (!credit_actual.contains(credit_expected))
		{
			testExeption("Actual credits: " + credit_actual + "not contains expected " + credit_expected + " credits!");
		}
		else
		{
			log().info("Actual credits: " + credit_actual + " == Expected credits " + credit_expected);
		}
	}

	/**
	 * Check pay status
	 * @param p_permission_type
	 * @param p_userID
	 */
	public void checkPayStatus(int p_permission_type, String p_userID)
	{
		if (manager.isFGF())
		{
			this.checkPayCredits(p_userID, manager.testUser().getCredits());
		}
		else if (manager.isCNF() || manager.isFlirt() || manager.isLGW() || manager.isWB() || manager.isBN())
		{
			this.checkPayStatusBase(p_permission_type, p_userID);
		}
	}

	public void checkPayStatus(TestUserDataModel p_user)
	{
		if (manager.isFGF())
		{
			this.checkPayCredits(p_user.getId(), p_user.getCredits());
		}
		else if (manager.isCNF() || manager.isFlirt() || manager.isLGW() || manager.isWB() || manager.isBN())
		{
			this.checkPayStatusBase(p_user.getMembershipStatus(), p_user.getId());
		}
	}

	/**
	 * To make user paid status
	 */
	@Deprecated
	public void makeUserPaid()
	{
		wd().waitForPresence(elements().blockUserID, "Block User ID doesn't appear");
		wd().wait(2);
		wd().waitForPresence(elements().btnPackagePermission, "Block Package permission doesn't appear");
		wd().click(elements().btnPackagePermission);
		wd().wait(2);

		String countButtonOn = wd().getTextByJs("return $('.permission.btn.btn-success.on').size()");
		log().warn("Count of GREEN (succes) button: " + countButtonOn);

		if (Integer.parseInt(countButtonOn) == 1)
		{
			log().info("User Paid status permission applied successfully: " + countButtonOn);
			manager.testUser().setMembershipStatus(1);
		}
		else
		{
			throw new TestException("User Paid status permission didn't applied: " + countButtonOn);
		}
	}

	public String getUserEmailByID(String p_userID)
	{
		log().info("Getting user email...");
		Map<String, String> userData = new HashMap<String, String>();
		userData = this.getUserData(p_userID);

		return userData.get("Email");
	}

	@Deprecated
	public void setStatusHot(TestUserDataModel p_user)
	{
		try
		{
			if (!this.isLogged())
			{
				throw new TestException("Unable to login after 5 attempts");
			}

			manager.httpURLConnection()
				.getPageContent(
					manager.config().BACKEND_URL_LIVE + "/user/markHot" + "?userId=" + URLEncoder.encode(p_user.getId(), "UTF-8") + "&siteId="
						+ URLEncoder
						.encode(p_user.getSiteId(), "UTF-8"));
		}
		catch (Exception p_ex)
		{
			throw new TestException("Cannot mark user as test in Backends: " + p_ex);
		}
	}

	public boolean loginToBackend()
	{
		try
		{
			CookieHandler.setDefault(new CookieManager());
			String page = manager.httpURLConnection().getPageContent(manager.config().BACKEND_LOGIN_URL);
			String postParams = manager.httpURLConnection().getLoginFormParams(page, manager.config().BACKEND_USERNAME, manager.config().BACKEND_PASSWORD);
			manager.httpURLConnection().sendPost(manager.config().BACKEND_LOGIN_URL, postParams);
			return true;
		}
		catch (Exception p_ex)
		{
			manager.log().warn("Cannot login to Backends: " + p_ex.toString());
			return false;
		}
	}

	public void setUserAsTest(String p_userID)
	{
		try
		{
			log().info("Mark user as test..");
			wd().wait(3);
			manager.httpURLConnection()
				.getPageContent(manager.config().BACKEND_URL_LIVE + "/user/markTester" + "?userId=" + URLEncoder.encode(p_userID, "UTF-8"));
			wd().wait(3);
		}
		catch (Exception p_ex)
		{
			throw new TestException("Cannot mark user as test in Backends: " + p_ex);
		}
	}

	public void unsetUserAsTest(String p_userID)
	{
		try
		{
			log().info("Mark user as NOT test..");
			wd().wait(3);
			manager.httpURLConnection()
				.getPageContent(manager.config().BACKEND_URL_LIVE + "/user/markNotTester" + "?userId=" + URLEncoder.encode(p_userID, "UTF-8"));
			wd().wait(3);
		}
		catch (Exception p_ex)
		{
			throw new TestException("Cannot mark user as test in Backends: " + p_ex);
		}
	}

	public String findUser(String p_user)
	{
		try
		{
			manager.httpURLConnection().getPageContent(manager.config().BACKEND_SEARCH_URL);
			return manager.httpURLConnection()
				.getPageContent(manager.config().BACKEND_SEARCH_URL + "?user_id=" + URLEncoder.encode(p_user, "UTF-8"));
		}
		catch (Exception p_ex)
		{
			throw new TestException("Error in POST REQUEST: " + p_ex);
		}
	}

	public String findUserCredits(String p_user)
	{
		try
		{
			manager.httpURLConnection().getPageContent(manager.config().BACKEND_SEARCH_URL);
			return manager.httpURLConnection().getPageContent(
				manager.config().BACKEND_URL_BASE + "/userCredit/history" + "?userId=" + URLEncoder.encode(p_user, "UTF-8"));
		}
		catch (Exception p_ex)
		{
			throw new TestException("Error in POST REQUEST: " + p_ex);
		}
	}

	public boolean isHot(String p_userId)
	{
		try
		{
			String html = this.findUser(p_userId);
			Document doc = Jsoup.parse(html);
			Elements links = doc.select("a[href]");

			for (Element link : links)
			{
				if (link.text().contains("Unset hot"))
				{
					return true;
				}
			}
			return false;
		}
		catch (Exception p_ex)
		{
			throw new TestException("Unable to check HOT status " + p_ex.toString());
		}
	}

	public Map<String, String> getUserData(String p_user)
	{
		Map<String, String> userData = new HashMap<String, String>();

		if (!this.isLogged())
		{
			throw new TestException("Unable to login after 5 attempts");
		}

		String html = this.findUser(p_user);

		Document doc;
		Element table;
		Element body;
		Elements rows;

		try
		{
			doc = Jsoup.parse(html);
			table = doc.getElementById("yw1");
			body = table.children().first();
			rows = body.children();
		}
		catch (Exception p_ex)
		{
			throw new TestException("Can't get rows from user info table!" + p_ex);
		}

		try
		{
			for (Element row : rows)
			{
				userData.put(row.children().get(0).text().replace(":", ""), row.children().get(1).text());
			}

			if (doc.getElementsByAttributeValueContaining("href", "markTester").size() > 0)
			{
				if (Config.BUILD.equals(Config.BUILD_LIVE))
				{
					this.setUserAsTest(userData.get("Id"));
				}
			}
		}
		catch (Exception p_ex)
		{
			throw new TestException("Can't get cell data from user info table!" + p_ex);
		}

		return userData;
	}

	public boolean isUserPaid(String p_userID, boolean do_pay)
	{
		Elements buttons = null;

		try
		{
			if (!this.isLogged())
			{
				throw new TestException("Unable to login after 5 attempts");
			}

			String html = findUser(p_userID);
			Document doc = Jsoup.parse(html);
			buttons = doc.getElementsByClass("permission");
			log().info("Count of GREEN (succes) button: " + doc.getElementsByAttributeValueContaining("class", "success").size());
		}
		catch (Exception p_ex)
		{
			throw new TestException("Can't get data from permissoins button! " + p_ex);
		}

		log().info("Check is user already has permission...");

		try
		{
			if (!buttons.get(0).attr("class").contains("btn-success"))
			{
				log().info("No, user doesn't have a package permission 1!");
				if (do_pay)
				{
					log().info("Make user a package permission 1...");
					try
					{
						manager.httpURLConnection().getPageContent(
							manager.config().BACKEND_PAY_URL + "?user_id=" + URLEncoder.encode(p_userID, "UTF-8") + "&action=enable&permission_id=1");
						manager.testUser().setMembershipStatus(1);

					}
					catch (Exception p_ex)
					{
						throw new TestException("Cannot mark user as paid: " + p_ex);
					}

					return true;
				}
				return false;
			}
			else
			{
				log().info("Yes, user doesn't have a package permission 1!");
				return true;
			}
		}
		catch (Exception p_ex)
		{
			throw new TestException("Can't check is user paid or not!" + p_ex);
		}
	}

	public void verifyPayStatus(TestUserDataModel p_user)
	{
		Elements buttons = null;
		String user_id = p_user.getId();
		try
		{
			if (!this.isLogged())
			{
				throw new TestException("Unable to login after 5 attempts");
			}

			String html = this.findUser(user_id);
			Document doc = Jsoup.parse(html);
			buttons = doc.getElementsByClass("permission");
		}
		catch (Exception p_ex)
		{
			manager.log().warn("Can't get data from permissoins buutton! " + p_ex);
		}

		try
		{
			Boolean v_package = buttons.get(0).attr("class").contains("btn-success");
			Boolean v_freeCommunication = buttons.get(1).attr("class").contains("btn-success");
			Boolean v_topInSearch = buttons.get(2).attr("class").contains("btn-success");
			Boolean v_fullPermissions = buttons.get(3).attr("class").contains("btn-success");

			switch (p_user.getMembershipStatus())
			{
				case 0:

					if (v_package)
					{
						manager.httpURLConnection().getPageContent(
							manager.config().BACKEND_PAY_URL + "?user_id=" + URLEncoder.encode(user_id, "UTF-8") + "&action=disable&permission_id=1");
					}

					if (v_freeCommunication)
					{
						manager.httpURLConnection().getPageContent(
							manager.config().BACKEND_PAY_URL + "?user_id=" + URLEncoder.encode(user_id, "UTF-8") + "&action=disable&permission_id=2");
					}

					if (v_topInSearch)
					{
						manager.httpURLConnection().getPageContent(
							manager.config().BACKEND_PAY_URL + "?user_id=" + URLEncoder.encode(user_id, "UTF-8") + "&action=disable&permission_id=7");
					}

					if (v_fullPermissions)
					{
						manager.httpURLConnection().getPageContent(
							manager.config().BACKEND_PAY_URL + "?user_id=" + URLEncoder.encode(user_id, "UTF-8") + "&action=disable&permission_id=5");
					}

					break;

				case 1:

					if (!v_package)
					{
						manager.httpURLConnection().getPageContent(
							manager.config().BACKEND_PAY_URL + "?user_id=" + URLEncoder.encode(user_id, "UTF-8") + "&action=enable&permission_id=1");
					}

					if (v_freeCommunication)
					{
						manager.httpURLConnection().getPageContent(
							manager.config().BACKEND_PAY_URL + "?user_id=" + URLEncoder.encode(user_id, "UTF-8") + "&action=disable&permission_id=2");
					}

					if (v_topInSearch)
					{
						manager.httpURLConnection().getPageContent(
							manager.config().BACKEND_PAY_URL + "?user_id=" + URLEncoder.encode(user_id, "UTF-8") + "&action=disable&permission_id=7");
					}

					if (v_fullPermissions)
					{
						manager.httpURLConnection().getPageContent(
							manager.config().BACKEND_PAY_URL + "?user_id=" + URLEncoder.encode(user_id, "UTF-8") + "&action=disable&permission_id=5");
					}

					break;

				case 2:

					if (!v_package)
					{
						manager.httpURLConnection().getPageContent(
							manager.config().BACKEND_PAY_URL + "?user_id=" + URLEncoder.encode(user_id, "UTF-8") + "&action=enable&permission_id=1");
					}

					if (!v_freeCommunication)
					{
						manager.httpURLConnection().getPageContent(
							manager.config().BACKEND_PAY_URL + "?user_id=" + URLEncoder.encode(user_id, "UTF-8") + "&action=enable&permission_id=2");
					}

					if (v_topInSearch)
					{
						manager.httpURLConnection().getPageContent(
							manager.config().BACKEND_PAY_URL + "?user_id=" + URLEncoder.encode(user_id, "UTF-8") + "&action=disable&permission_id=7");
					}

					if (v_fullPermissions)
					{
						manager.httpURLConnection().getPageContent(
							manager.config().BACKEND_PAY_URL + "?user_id=" + URLEncoder.encode(user_id, "UTF-8") + "&action=disable&permission_id=5");
					}

					break;

				case 3:

					if (!v_package)
					{
						manager.httpURLConnection().getPageContent(
							manager.config().BACKEND_PAY_URL + "?user_id=" + URLEncoder.encode(user_id, "UTF-8") + "&action=enable&permission_id=1");
					}

					if (v_freeCommunication)
					{
						manager.httpURLConnection().getPageContent(
							manager.config().BACKEND_PAY_URL + "?user_id=" + URLEncoder.encode(user_id, "UTF-8") + "&action=disable&permission_id=2");
					}

					if (!v_topInSearch)
					{
						manager.httpURLConnection().getPageContent(
							manager.config().BACKEND_PAY_URL + "?user_id=" + URLEncoder.encode(user_id, "UTF-8") + "&action=enable&permission_id=7");
					}

					if (v_fullPermissions)
					{
						manager.httpURLConnection().getPageContent(
							manager.config().BACKEND_PAY_URL + "?user_id=" + URLEncoder.encode(user_id, "UTF-8") + "&action=disable&permission_id=5");
					}

					break;

				case 4:
					if (!v_package)
					{
						manager.httpURLConnection().getPageContent(
							manager.config().BACKEND_PAY_URL + "?user_id=" + URLEncoder.encode(user_id, "UTF-8") + "&action=enable&permission_id=1");
					}

					if (v_freeCommunication)
					{
						manager.httpURLConnection().getPageContent(
							manager.config().BACKEND_PAY_URL + "?user_id=" + URLEncoder.encode(user_id, "UTF-8") + "&action=disable&permission_id=2");
					}

					if (v_topInSearch)
					{
						manager.httpURLConnection().getPageContent(
							manager.config().BACKEND_PAY_URL + "?user_id=" + URLEncoder.encode(user_id, "UTF-8") + "&action=disable&permission_id=7");
					}

					if (!v_fullPermissions)
					{
						manager.httpURLConnection().getPageContent(
							manager.config().BACKEND_PAY_URL + "?user_id=" + URLEncoder.encode(user_id, "UTF-8") + "&action=enable&permission_id=5");
					}
					break;
			}
		}
		catch (Exception p_ex)
		{
			manager.log().warn("Something went wrong in Verify pay status proccess...");
		}
	}

	public void checkPayStatusBase(int p_permission_type, String p_userID)
	{
		try
		{
			if (!this.isLogged())
			{
				throw new TestException("Unable to login after 5 attempts");
			}

			String html = findUser(p_userID);
			Document doc = Jsoup.parse(html);
			String countButtonOn = "";
			Elements buttons = null;

			buttons = doc.getElementsByClass("permission");
			countButtonOn = Integer.toString(doc.getElementsByClass("btn-success").size());

			switch (p_permission_type)
			{
				case 0:
				{
					if (Integer.parseInt(countButtonOn) != 0)
					{
						throw new TestException("User may be FREE, but was found >0 permissions for him");
					}
					else
					{
						log().info("User is FREE. Tested OK!");
					}
					break;
				}
				case 1:
				{
					if (Integer.parseInt(countButtonOn) == 0)
					{
						throw new TestException("User must have 1 permission, but now he is FREE user");
					}

					if (Integer.parseInt(countButtonOn) > 1)
					{
						// allow only specific 2 green button
						if (!buttons.get(4).attr("class").contains("btn-success"))
						{
							throw new TestException("User must have only 1 'Package permissions', but was found permissions: " + countButtonOn);
						}
					}

					if (!buttons.get(0).attr("class").contains("btn-success"))
					{
						log().info("Class of button: " + buttons.get(0).attr("class"));
						throw new TestException("User must have 1 'Package Permission' permission, but it permissions is not active for him");
					}
					else
					{
						log().info("User is paid. Permission is 1. Tested OK!");
					}
					break;
				}
				case 2:
				{
					if (Integer.parseInt(countButtonOn) == 0)
					{
						throw new TestException("User must have minimum 1 permission, but now he is FREE user");
					}

					if (!buttons.get(1).attr("class").contains("btn-success"))
					{
						log().info("Class of button: " + buttons.get(1).attr("class"));
						throw new TestException("User must have 2 permission 'Feature communication', but it permissions is not active for him");
					}
					else
					{
						log().info("Button with text 'Free communication; Special delivery;' is ACTIVE");
					}
					break;
				}
				case 3:
				{
					if (Integer.parseInt(countButtonOn) == 0)
					{
						throw new TestException("User must have minimum 1 permission, but now he is FREE user");
					}
					if (!buttons.get(2).attr("class").contains("btn-success"))
					{
						log().info("Class of button: " + buttons.get(2).attr("class").contains("btn-success"));
						throw new TestException("User must have 3 permission 'Feature search', but it permissions is not active for him");
					}
					else
					{
						log().info("Button with text 'Highlight profile; Invisible mode; Top in search;' is ACTIVE");
					}
					break;
				}
				case 4:
				{
					if (Integer.parseInt(countButtonOn) == 0)
					{
						throw new TestException("User must have minimum 1 permission, but now he is FREE user");
					}
					if (!buttons.get(3).attr("class").contains("btn-success"))
					{
						log().info("Class of button: " + buttons.get(3).attr("class").contains("btn-success"));
						throw new TestException("User must have permission 'Feature search + Feature communication', but it permissions is not active for him");
					}
					else
					{
						log().info("Button with text 'Free communication; Special delivery; Highlight profile; Invisible mode; Top in search;' is ACTIVE");
					}
					break;
				}
			}
		}
		catch (Exception p_ex)
		{
			manager.log().error("Can't get data from permissions buttons in admin panel!" + p_ex);
		}
	}

	public void checkPayCredits(String p_userID, String p_expectedCredits)
	{
		try
		{

			if (!this.isLogged())
			{
				throw new TestException("Unable to login after 5 attempts");
			}

			String html = findUserCredits(p_userID);
			Document doc = Jsoup.parse(html);
			String credit_actual = "";

			if (doc.getElementsByClass("empty").size() > 0)
			{
				throw new TestException("EMPTY CREDIT HISTORY: " + doc.getElementsByClass("empty").get(1).text());
			}

			Elements tbody = doc.getElementsByTag("tbody");
			credit_actual = tbody.get(0).child(0).child(4).text();
			credit_actual = manager.string().getSubstringBefore(credit_actual, ".");

			if (!credit_actual.equals(p_expectedCredits))
			{
				throw new TestException("Payed credits: " + p_expectedCredits + ", but in admin panel displayed last order on " + credit_actual + " credits!");
			}
			else
			{
				log().info("Actual credits: " + credit_actual + " == Expected credits " + p_expectedCredits);
			}
		}
		catch (Exception p_ex)
		{
			manager.log().warn("Can't get a text data from tables elements! " + p_ex);
		}

	}

	public void setUserAsHot(TestUserDataModel p_userModel)
	{
		try
		{
			log().info("set status hot");

			if (!this.isLogged())
			{
				throw new TestException("Unable to login after 5 attempts");
			}
			if (!isHot(p_userModel.getId()))
			{
				manager.httpURLConnection().getPageContent(
					manager.config().BACKEND_URL_LIVE + "/user/markHot" + "?userId=" + URLEncoder.encode(p_userModel.getId(), "UTF-8")
						+ "&siteId=" + URLEncoder.encode(p_userModel.getSiteId(), "UTF-8"));
			}
			else
			{
				manager.log().info(p_userModel.getId() + " is already HOT");
			}
			wd().wait(2); // Wait for data sync
		}
		catch (Exception p_ex)
		{
			manager.log().warn("setUserAsHot: " + p_ex);
		}
	}

	public void unsetUserAsHot(TestUserDataModel p_userModel)
	{
		try
		{
			if (!this.isLogged())
			{
				throw new TestException("Unable to login after 5 attempts");
			}

			if (isHot(p_userModel.getId()))
			{
				manager.httpURLConnection().getPageContent(
					manager.config().BACKEND_URL_LIVE + "/user/markNotHot" + "?userId=" + URLEncoder.encode(p_userModel.getId(), "UTF-8")
						+ "&siteId=" + URLEncoder.encode(p_userModel.getSiteId(), "UTF-8"));
				wd().wait(2); // Wait for data sync
			}
			else
			{
				manager.log().info(p_userModel.getId() + " is already NOT HOT");
			}
		}
		catch (Exception p_ex)
		{
			manager.log().warn("unsetUserAsHot: " + p_ex);
		}
	}

	public void setUserAsScammer(TestUserDataModel p_userModel)
	{
		try
		{
			if (!this.isLogged())
			{
				throw new TestException("Unable to login after 5 attempts");
			}

			manager.httpURLConnection().getRequest(
				manager.config().BACKEND_URL_LIVE + "/admin/scammer/handling_scammer?action=markasacammer&user_id=" + URLEncoder
					.encode(p_userModel.getId(), "UTF-8")
					+ "&reason_id=23");
			wd().wait(2); // Wait for data sync
		}
		catch (Exception p_ex)
		{
			manager.log().warn("setUserAsScammer: " + p_ex);
		}
	}

	public void unsetUserAsScammer(TestUserDataModel p_userModel)
	{
		try
		{
			if (!this.isLogged())
			{
				throw new TestException("Unable to login after 5 attempts");
			}

			this.unsetUserAsTest(p_userModel.getId());

			manager.httpURLConnection().getRequest(
				manager.config().BACKEND_URL_LIVE + "/admin/scammer/handling_scammer?action=markasnotscammer&user_id=" + URLEncoder
					.encode(p_userModel.getId(), "UTF-8")
					+ "&reason_id=23");
			wd().wait(2); // Wait for data sync

			this.setUserAsTest(p_userModel.getId());
		}
		catch (Exception p_ex)
		{
			manager.log().warn("setUserAsScammer: " + p_ex);
		}
	}

}


